package org.example.music_manage_system.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;

import java.io.InputStream;

public class AliOssUtil {
    private static final String ENDPOINT = "https://oss-cn-beijing.aliyuncs.com";
    private static final String ACCESS_KEY_ID = "LTAI5tLV5iDEUH975jYVogoK";
    private static final String ACCESS_KEY_SECRET = "K1jSluJSQQcRAcIpQL1QJIVU9GpXBr";
    private static final String BUCKETNAME = "music-manage-system";

    public static String uploadFile(String objectName, InputStream in) throws Exception {
        //文件的名字和文件的输入地址要变，所以要把它两抽象出来
        OSS ossClient = (new OSSClientBuilder()).build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        String url = "";
        try {
            String content = "Hello OSS，你好世界";
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKETNAME, objectName, in);
            ossClient.putObject(putObjectRequest);
            url = "https://"+BUCKETNAME+"." + ENDPOINT.substring(ENDPOINT.lastIndexOf("/")+1)+"/"+objectName;
        } catch (OSSException var14) {
            OSSException oe = var14;
            System.out.println("Caught an OSSException, which means your request made it to OSS, but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException var15) {
            ClientException ce = var15;
            System.out.println("Caught an ClientException, which means the client encountered a serious internal problem while trying to communicate with OSS, such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }

        }

    return url;
    }
}
